package group.sicher.log.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Donald
 * @description
 * @date 2019-08-02
 **/
public class TraceUtil {
    private static final ThreadLocal<String> CONTEXT = new ThreadLocal<>();

    private TraceUtil() {
        throw new AssertionError();
    }

    public static String getRequestBody(HttpServletRequest request) {
        try {
            ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
            return IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
        } catch (IOException e) {
        }

        return "";
    }

    public static String getResponseBody(HttpServletResponse response) {
        try {
            ContentCachingResponseWrapper wrapper = (ContentCachingResponseWrapper) response;
            return IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
        } catch (IOException e) {
        }

        return "";
    }

    public static String getRequestParameters(HttpServletRequest request) {
        return JsonUtil.toJson(request.getParameterMap());
    }

    public static String getRequestHeaders(HttpServletRequest request) {
        Map<String, String> header = new HashMap<>();
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            header.put(name, request.getHeader(name));
        }
        return JsonUtil.toJson(header);
    }

    public static String getRequestId() {
        String id = CONTEXT.get();
        if (id == null) {
            id = System.currentTimeMillis() + RandomStringUtils.randomNumeric(4);
            CONTEXT.set(id);
        }
        return id;
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
